<?php

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 * Description of Status
 *
 * @author QuangKhoi
 */
class Status {

    public $code;
    public $message;
    public $id;
    public $userid;
    public $title;
    public $content;
    public $datecreated;
    public $likes;
    public $replies;
    public $visibility;
    public $sharedfrom;
    public $view;
    public $photo;
    public $user;
    public $avatar;

    public function __construct() {
        
    }

    public static function withCode($code, $message) {
        $instance = new self();
        $instance->code = $code;
        $instance->message = $message;
        return $instance;
    }

    public static function withId($id) {
        $instance = new self();
        $db = Database::getInstance();
        $query = "SELECT * FROM statuses WHERE id = '$id'";
        $result = $db->query($query);
        if ($result->returnCode == 200) {
            if ($result->dataTable->numOfRow == 1) {
                $rows = $result->dataTable->rows[0];
                $instance->id = $rows["id"];
                $instance->userid = $rows["userid"];
                $instance->title = $rows["title"];
                $instance->content = $rows["content"];
                $instance->datecreated = $rows["datecreated"];
                $instance->likes = $rows["likes"];
                $instance->replies = $rows["replies"];
                $instance->visibility = $rows["visibility"];
                $instance->sharedfrom = $rows["sharedfrom"];
                $instance->view = $rows["view"];
                $instance->photo = Photo::withId(Photo::getPhotoId($rows['id']));
                $instance->user = User::withId($rows['userid']);
                if ($instance->user->avatarid)
                    $instance->avatar = Photo::withId($instance->user->avatarid);
                else {
                    $instance->avatar = new Photo();
                    $instance->avatar->url = "Images/avatar-default.gif";
                }
                return $instance;
            } else {
                return NULL;
            }
        } else {
            $controller = new ErrorController(new Error(), new ErrorView, $result->returnCode, $result->message);
            $controller->returnError();
            return NULL;
        }
    }

    public static function withData($userid, $title, $content, $datecreated) {
        $instance = new self();
        $instance->userid = $userid;
        $instance->title = $title;
        $instance->content = $content;
        $instance->datecreated = $datecreated;
        return $instance;
    }

    public static function getStatusId($photoid) {
        $db = Database::getInstance();
        $query = "SELECT * FROM photodata WHERE photoid='$photoid'";
        $result = $db->query($query);
        if ($result->returnCode == 200) {
            if ($result->dataTable->numOfRow == 1) {
                $row = $result->dataTable->rows[0];
                return $row["statusid"];
            } else {
                return NULL;
            }
        } else {
            return NULL;
        }
    }

    public static function addStatus($status) {
        $db = Database::getInstance();
        $id = $db->getID("statuses", "id");
        $userid = $status->userid;
        $title = $status->title;
        $content = $status->content;
        $datecreated = $status->datecreated;
        $likes = 0;
        $replies = 0;
        $visibility = 1;
        $sharedfrom = 0;
        $view = 0;
        $query = "INSERT INTO statuses (id, userid, title, content, datecreated, likes, 
                replies, visibility, sharedfrom, view) VALUES 
                ('$id', '$userid', '$title', '$content', '$datecreated', '$likes', '$replies', 
                '$visibility', '$sharedfrom', '$view')";
        $result = $db->query($query);
        if ($result->returnCode == 200) {
            return $id;
        } else {
            return false;
        }
    }

    public static function deleteStatus($statusid) {
        $db = Database::getInstance();
        $query = "DELETE FROM statuses WHERE id = '$statusid'";
        $result = $db->query($query);
        if ($result->returnCode == 200) {
            $query = "DELETE FROM likes WHERE statusid = '$statusid'";
            $result = $db->query($query);
            if ($result->returnCode == 200) {
                $query = "DELETE FROM replies WHERE statusid = '$statusid'";
                $result = $db->query($query);
                if ($result->returnCode == 200) {
                    return "true";
                } else {
                    $controller = new ErrorController(new Error(), new ErrorView, $result->returnCode, $result->message);
                    $controller->returnError();
                    return "false";
                }
            } else {
                $controller = new ErrorController(new Error(), new ErrorView, $result->returnCode, $result->message);
                $controller->returnError();
                return "false";
            }
        } else {
            $controller = new ErrorController(new Error(), new ErrorView, $result->returnCode, $result->message);
            $controller->returnError();
            return "false";
        }
    }

    public static function getPopularStatuses($position, $length, $date = '', $orderBy = 'view') {
        $db = Database::getInstance();
        if ($date)
            $query = "SELECT id FROM statuses WHERE datecreated = '$date' ORDER BY $orderBy desc LIMIT $position, $length";
        else
            $query = "SELECT id FROM statuses ORDER BY $orderBy desc LIMIT $position, $length";
        $result = $db->query($query);
        if ($result->returnCode == 200) {
            if ($result->dataTable->numOfRow > 0) {
                $res = array();
                foreach ($result->dataTable->rows as $row) {
                    $res[] = Status::withId($row['id']);
                }
                return $res;
            }
            else
                return false;
        }
        else
            return false;
    }

    public static function getEveryoneStatuses($position, $length, $date = '') {
        $db = Database::getInstance();
        if ($date)
            $query = "SELECT id FROM statuses WHERE datecreated = '$date' ORDER BY RAND() LIMIT $position, $length";
        else
            $query = "SELECT id FROM statuses ORDER BY RAND() LIMIT $position, $length";
        $result = $db->query($query);
        if ($result->returnCode == 200) {
            if ($result->dataTable->numOfRow > 0) {
                $res = array();
                foreach ($result->dataTable->rows as $row) {
                    $res[] = Status::withId($row['id']);
                }
                return $res;
            }
            else
                return false;
        }
        else
            return false;
    }

    public static function getDebutsStatuses($position, $length, $date = '') {
        $db = Database::getInstance();
        if ($date)
            $query = "SELECT id FROM statuses WHERE datecreated = '$date' ORDER BY datecreated desc LIMIT $position, $length";
        else
            $query = "SELECT id FROM statuses ORDER BY datecreated desc LIMIT $position, $length";
        $result = $db->query($query);
        if ($result->returnCode == 200) {
            if ($result->dataTable->numOfRow > 0) {
                $res = array();
                foreach ($result->dataTable->rows as $row) {
                    $res[] = Status::withId($row['id']);
                }
                return $res;
            }
            else
                return false;
        }
        else
            return false;
    }

    public static function getUserStatuses($position, $length, $user, $date = '') {
        $db = Database::getInstance();
        if ($date)
            $query = "SELECT id FROM statuses WHERE datecreated = '$date' AND userid = $user->id ORDER BY RAND() desc LIMIT $position, $length";
        else
            $query = "SELECT id FROM statuses WHERE userid = $user->id ORDER BY RAND() desc LIMIT $position, $length";
        $result = $db->query($query);
        if ($result->returnCode == 200) {
            if ($result->dataTable->numOfRow > 0) {
                $res = array();
                foreach ($result->dataTable->rows as $row) {
                    $res[] = Status::withId($row['id']);
                }
                return $res;
            }
            else
                return false;
        }
        else
            return false;
    }

    public static function getUserLikedStatuses($position, $length, $user, $date = '') {
        $db = Database::getInstance();
        if ($date)
            $query = "SELECT statuses.id FROM statuses LEFT JOIN likes ON likes.statusid = statuses.id WHERE statuses.datecreated = '$date' AND likes.userid = $user->id ORDER BY likes.datecreated desc LIMIT $position, $length";
        else
            $query = "SELECT statuses.id FROM statuses LEFT JOIN likes ON likes.statusid = statuses.id WHERE likes.userid = $user->id ORDER BY likes.datecreated desc LIMIT $position, $length";
        $result = $db->query($query);
        if ($result->returnCode == 200) {
            if ($result->dataTable->numOfRow > 0) {
                $res = array();
                foreach ($result->dataTable->rows as $row) {
                    $res[] = Status::withId($row['id']);
                }
                return $res;
            }
            else
                return false;
        }
        else
            return false;
    }

    public static function getPhotoCount($length, $date = NULL) {
        if ($length != 0) {
            $db = Database::getInstance();
            if ($date)
                $query = "SELECT COUNT(id) FROM statuses WHERE datecreated='$date'";
            else
                $query = "SELECT COUNT(id) FROM statuses";
            $result = $db->query($query);
            if ($result->returnCode == 200) {
                if ($result->dataTable->numOfRow == 1) {
                    $max = $result->dataTable->rows[0];
                    $page = $max[0] / $length;
                    return ceil($page);
                }
                else
                    return false;
            }
            else
                return false;
        }else {
            return false;
        }
    }

    public static function getUserPhotoCount($length, $user, $date = NULL, $like = 0) {
        if ($length != 0) {
            $db = Database::getInstance();
            if ($date)
                if ($like)
                    $query = "SELECT count(*) FROM statuses LEFT JOIN likes ON likes.statusid = statuses.id  WHERE datecreated='$date' AND likes.userid = $user->id";
                else
                    $query = "SELECT COUNT(id) FROM statuses WHERE datecreated='$date' AND userid = $user->id";
            else
            if ($like)
                $query = "SELECT count(*) FROM statuses LEFT JOIN likes ON likes.statusid = statuses.id  FROM statuses WHERE likes.userid = $user->id";
            else
                $query = "SELECT COUNT(id) FROM statuses WHERE userid = $user->id";
            $result = $db->query($query);
            if ($result->returnCode == 200) {
                if ($result->dataTable->numOfRow == 1) {
                    $max = $result->dataTable->rows[0];
                    $page = $max[0] / $length;
                    return ceil($page);
                }
                else
                    return false;
            }
            else
                return false;
        }else {
            return false;
        }
    }

    public static function updateColumn($column, $value, $statusid) {
        $db = Database::getInstance();
        $query = "UPDATE statuses SET $column='$value' WHERE id='$statusid'";
        $result = $db->query($query);
        if ($result->returnCode == 200) {
            return "true";
        } else {
            $controller = new ErrorController(new Error(), new ErrorView, $result->returnCode, $result->message);
            $controller->returnError();
            return "false";
        }
    }

    public function increaseView() {
        $value = $this->view + 1;
        Status::updateColumn("view", $value, $this->id);
        $this->view++;
    }

    public static function getAmountStatus($userid) {
        $db = Database::getInstance();
        $query = "SELECT COUNT(id) FROM statuses WHERE userid='$userid'";
        $result = $db->query($query);
        if ($result->returnCode == 200) {
            if ($result->dataTable->numOfRow == 1) {
                $count = $result->dataTable->rows[0];
                return $count[0];
            }
            else
                return -1;
        }
        else
            return -1;
    }

    public static function getStatusTopViewUser($userid) {
        $db = Database::getInstance();
        $query = "SELECT id FROM statuses ORDER BY view desc LIMIT 0, 1 WHERE userid='$userid'";
        $result = $db->query($query);
        if ($result->returnCode == 200) {
            if ($result->dataTable->numOfRow == 1) {
                $row = $result->dataTable->rows[0];
                return $row['id'];
            }
            else
                return false;
        }
        else
            return false;
    }

    public static function getStatusByView($position, $length) {
        $db = Database::getInstance();
        $query = "SELECT id FROM statuses ORDER BY view desc LIMIT $position, $length";
        $result = $db->query($query);
        if ($result->returnCode == 200) {
            if ($result->dataTable->numOfRow > 0) {
                foreach ($result->dataTable->rows as $row) {
                    $id = $row['id'];
                    $res = "$res$id;";
                }
                return $res;
            }
            else
                return false;
        }
        else
            return false;
    }

    public static function getStatusByViewByDate($position, $length, $date) {
        $db = Database::getInstance();
        $query = "SELECT id FROM statuses WHERE datecreated = '$date' ORDER BY view desc LIMIT $position, $length";
        $result = $db->query($query);
        if ($result->returnCode == 200) {
            if ($result->dataTable->numOfRow > 0) {
                foreach ($result->dataTable->rows as $row) {
                    $id = $row['id'];
                    $res = "$res$id;";
                }
                return $res;
            }
            else
                return false;
        }
        else
            return false;
    }

    public static function getStatus($position, $length) {
        $db = Database::getInstance();
        $query = "SELECT id FROM statuses LIMIT $position, $length";
        $result = $db->query($query);
        if ($result->returnCode == 200) {
            if ($result->dataTable->numOfRow > 0) {
                foreach ($result->dataTable->rows as $row) {
                    $id = $row['id'];
                    $res = "$res$id;";
                }
                return $res;
            }
            else
                return false;
        }
        else
            return false;
    }

    public static function getStatusByDate($position, $length, $date = NULL) {
        $db = Database::getInstance();
        if ($date)
            $query = "SELECT id FROM statuses WHERE datecreated = '$date' LIMIT $position, $length";
        else
            $query = "SELECT id FROM statuses ORDER BY datecreated desc LIMIT $position, $length";
        $result = $db->query($query);
        if ($result->returnCode == 200) {
            if ($result->dataTable->numOfRow > 0) {
                foreach ($result->dataTable->rows as $row) {
                    $id = $row['id'];
                    $res = "$res$id;";
                }
                return $res;
            }
            else
                return false;
        }
        else
            return false;
    }

    public static function getStatusTopView($date = NULL) {
        $db = Database::getInstance();
        if ($date)
            $query = "SELECT id FROM statuses WHERE datecreated = '$date' ORDER BY view desc LIMIT 0, 1";
        else
            $query = "SELECT id FROM statuses ORDER BY view desc LIMIT 0, 1";
        $result = $db->query($query);
        if ($result->returnCode == 200) {
            if ($result->dataTable->numOfRow == 1) {
                $row = $result->dataTable->rows[0];
                return $row['id'];
            }
            else
                return false;
        }
        else
            return false;
    }

    public static function getStatusTopNew($date = NULL) {
        $db = Database::getInstance();
        if ($date)
            $query = "SELECT id FROM statuses WHERE datecreated = '$date' ORDER BY datecreated desc LIMIT 0, 1";
        else
            $query = "SELECT id FROM statuses ORDER BY datecreated desc LIMIT 0, 1";
        $result = $db->query($query);
        if ($result->returnCode == 200) {
            if ($result->dataTable->numOfRow == 1) {
                $row = $result->dataTable->rows[0];
                return $row['id'];
            }
            else
                return false;
        }
        else
            return false;
    }

    public static function getMaxStatus($length, $date = NULL) {
        if ($length != 0) {
            $db = Database::getInstance();
            if ($date)
                $query = "SELECT COUNT(id) FROM statuses WHERE datecreated='$date'";
            else
                $query = "SELECT COUNT(id) FROM statuses";
            $result = $db->query($query);
            if ($result->returnCode == 200) {
                if ($result->dataTable->numOfRow == 1) {
                    $max = $result->dataTable->rows[0];
                    $page = $max[0] / $length;
                    return ceil($page);
                }
                else
                    return false;
            }
            else
                return false;
        }else {
            return false;
        }
    }

    public static function RGBToHex($r, $g, $b) {
        //String padding bug found and the solution put forth by Pete Williams (http://snipplr.com/users/PeteW)
        $hex = "#";
        $hex.= str_pad(dechex($r), 2, "0", STR_PAD_LEFT);
        $hex.= str_pad(dechex($g), 2, "0", STR_PAD_LEFT);
        $hex.= str_pad(dechex($b), 2, "0", STR_PAD_LEFT);

        return $hex;
    }

    public static function HexToRGB($hex) {
        $hex = ereg_replace("#", "", $hex);
        $color = array();

        if (strlen($hex) == 3) {
            $color['r'] = hexdec(substr($hex, 0, 1) . $r);
            $color['g'] = hexdec(substr($hex, 1, 1) . $g);
            $color['b'] = hexdec(substr($hex, 2, 1) . $b);
        } else if (strlen($hex) == 6) {
            $color['r'] = hexdec(substr($hex, 0, 2));
            $color['g'] = hexdec(substr($hex, 2, 2));
            $color['b'] = hexdec(substr($hex, 4, 2));
        }

        return $color;
    }

    public static function getByColors($color, $position, $length) {
        $db = Database::getInstance();
        $query = "SELECT statusid, hexcolor FROM photodata WHERE hexcolor = '#$color' ORDER BY RAND() desc LIMIT $position, $length";
        $result = $db->query($query);
        if ($result->returnCode == 200) {
            if ($result->dataTable->numOfRow > 0) {
                $res = array();
                foreach ($result->dataTable->rows as $row) {
                    $res[] = Status::withId($row['statusid']);
                }
                return $res;
            }
            else
                return false;
        }
        else
            return false;
    }

    public static function getColorPhotoCount($length, $date = NULL) {
        if ($length != 0) {
            $db = Database::getInstance();
            if ($date)
                $query = "SELECT COUNT(*) FROM statuses LEFT JOIN photos ON photos.id = statuses.id LEFT JOIN photocolors ON photocolors.photoid = photos.id WHERE hexcolor >= '$colorStart' AND hexcolor <= '$colorEnd' ORDER BY RAND() desc";
            else
                $query = "SELECT COUNT(*) FROM statuses LEFT JOIN photos ON photos.id = statuses.id LEFT JOIN photocolors ON photocolors.photoid = photos.id WHERE hexcolor >= '$colorStart' AND hexcolor <= '$colorEnd' ORDER BY RAND() desc";
            $result = $db->query($query);
            if ($result->returnCode == 200) {
                if ($result->dataTable->numOfRow == 1) {
                    $max = $result->dataTable->rows[0];
                    $page = $max[0] / $length;
                    return ceil($page);
                }
                else
                    return false;
            }
            else
                return false;
        }else {
            return false;
        }
    }

    public static function getFollowingStatuses($position = 0, $length = 12) {
        global $curUser;
        $userId = $curUser->id;
        $db = Database::getInstance();
        $query = "SELECT id FROM statuses WHERE userid IN (SELECT userid2 FROM relationships WHERE userid1 = $userId) ORDER BY datecreated LIMIT $position, $length";
        $result = $db->query($query);
        if ($result->returnCode == 200) {
            if ($result->dataTable->numOfRow > 0) {
                $res = array();
                foreach ($result->dataTable->rows as $row) {
                    $res[] = Status::withId($row['id']);
                }
                return $res;
            }
            else
                return false;
        }
        else
            return false;
    }

}

?>
